package com.sigmatrix.h5.service.impl.v2;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.sigmatrix.h5.auth.ILoginUser;
import com.sigmatrix.h5.entity.FirstLevelQRCode;
import com.sigmatrix.h5.entity.v2.Product;
import com.sigmatrix.h5.entity.v2.VerRecord;
import com.sigmatrix.h5.mapper.VerRecordMapper;
import com.sigmatrix.h5.service.AdminAreaService;
import com.sigmatrix.h5.service.FirstLevelQrCodeService;
import com.sigmatrix.h5.service.db.ProductDBService;
import com.sigmatrix.h5.service.v2.CommonDBService;
import com.sigmatrix.h5.utils.Assert;
import com.sigmatrix.h5.utils.CommonUtils;
import com.sigmatrix.h5.web.args.VerRecordParam;
import com.sigmatrix.h5.web.mode.SigmatrixLocationWrapper;

/**
 * 核销记录
 * 
 * @author jianwei.wang
 *
 */
@Service
public class VerRecordServiceImpl extends CommonDBService<VerRecordMapper, VerRecord> {
	
	@Autowired
	FirstLevelQrCodeService firstLevelQrCodeService;
	
	@Autowired
	ProductDBService productService;
	
	@Autowired
	private AdminAreaService adminAreaService;

	public boolean notVerfication(ILoginUser user, VerRecordParam vo) {
		//验证码奖品吗是否在一级码表中存在
		validateCode(user, vo);
		VerRecord record = new VerRecord();
		record.setTablePrefix("_" + user.getEseCode());
		record.setPrizeCode(vo.getPrizeCode());
		Integer count = baseMapper.selectCount(new EntityWrapper<VerRecord>(record));
		if (CommonUtils.isEmpty(count) || count <= 0) {
			return true;
		}
		return false;
	}
	@Transactional
	public VerRecord verification(ILoginUser user, VerRecordParam vo) {
		FirstLevelQRCode code = validateCode(user, vo);
		Assert.notNull(code.getProductId(), "码未绑定奖品不能使用");
		Product product = productService.selectById(code.getProductId(), "_"+user.getEseCode());
		Assert.notNull(product, "码绑定奖品不存在");
		VerRecord record = new VerRecord();
		record.setPrizeCode(vo.getPrizeCode());
		record.setVerificationDate(new Date());
		record.setType((short) 1);
		record.setStatus((short) 1);
		record.setTablePrefix("_" + user.getEseCode());
		record.setShopId(user.getDealerOrShopId());
		record.setUserId(user.getAcctId());
		record.setUserAccount(user.getLoginName());
		record.setPrizeId(code.getProductId());
		record.setPrizeName(product.getName());
		record.setNum(1);
		record.setLat(vo.getLat());
		record.setLng(vo.getLng());
		SigmatrixLocationWrapper location2 = adminAreaService.getLocation(vo.getLat(), vo.getLng());
		record.setProvince(location2.getProvince());
		record.setCity(location2.getCity());
		record.setCounty(location2.getCitySubOrdinate());
		baseMapper.insert(record);
		return record;
	}
	
	public FirstLevelQRCode validateCode(ILoginUser user, VerRecordParam vo) {
		FirstLevelQRCode code = firstLevelQrCodeService.findByQRCode(CommonUtils.encodeQRCode(vo.getPrizeCode()),user.getEseCode());
//		FirstLevelQRCode code = firstLevelQrCodeService.findByQRCode(vo.getPrizeCode(),user.getEseCode());
		Assert.notNull(code, "码不存在");
		return code;
	}

}
